package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.MessageEnum;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.error.CourseException;
import com.softer.wxzj.entity.BaseBuildInfo;
import com.softer.wxzj.entity.BaseSubRegionInfo;
import com.softer.wxzj.mapper.BaseBuildInfoMapper;
import com.softer.wxzj.mapper.BaseHouseInfoMapper;
import com.softer.wxzj.mapper.BaseSubRegionInfoMapper;
import com.softer.wxzj.service.BaseSubRegionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: wxzj
 * @author: wxr
 * @create: 2019-05-20 16:01
 **/
@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class BaseSubRegionInfoServiceImpl implements BaseSubRegionInfoService {
    @Autowired
    private BaseSubRegionInfoMapper baseSubRegionInfoMapper;
    @Autowired
    private BaseBuildInfoMapper baseBuildInfoMapper;
    @Autowired
    private BaseHouseInfoMapper baseHouseInfoMapper;

    @Override
    public CommonResult insert(BaseSubRegionInfo baseSubRegionInfo) {
        //去空格
        baseSubRegionInfo.setSubRegionName(baseSubRegionInfo.getSubRegionName().replaceAll(" ", ""));
        baseSubRegionInfo.setSubRegionNo(baseSubRegionInfo.getSubRegionNo().replaceAll(" ", ""));
        if (!baseSubRegionInfo.getSubRegionName().endsWith("区")) {
            baseSubRegionInfo.setSubRegionName(baseSubRegionInfo.getSubRegionName() + "区");
        }
        int no = baseSubRegionInfoMapper.selectSubRegionName(baseSubRegionInfo.getSubRegionName(), baseSubRegionInfo.getRegionNo()).size();
        if (no > 0) {
            return  CommonResult.ResponseResult(0, baseSubRegionInfo.getSubRegionName()+"分区名称已存在");
        }
        int num = baseSubRegionInfoMapper.selectSubRegionNo(baseSubRegionInfo.getSubRegionNo(), baseSubRegionInfo.getRegionNo()).size();
        //查看是否有存在的分区编号，若存在则不能添加
        if (num > 0) {
            return  CommonResult.ResponseResult(0, baseSubRegionInfo.getSubRegionNo()+"分区编号已存在");
        } else {
            String name = baseSubRegionInfo.getSubRegionName();
            if (!name.endsWith("区")) {
                baseSubRegionInfo.setSubRegionName(baseSubRegionInfo.getSubRegionName() + "区");
            }
            no = baseSubRegionInfoMapper.insert(baseSubRegionInfo);

            return CommonResult.ResponseResult(no);
        }
    }
    @Override
    public CommonResult update(BaseSubRegionInfo baseSubRegionInfo) {
        int no = 0;
        //去空格
        baseSubRegionInfo.setSubRegionName(baseSubRegionInfo.getSubRegionName().replaceAll(" ", ""));
        baseSubRegionInfo.setSubRegionNo(baseSubRegionInfo.getSubRegionNo().replaceAll(" ", ""));
        BaseSubRegionInfo oldBase = baseSubRegionInfoMapper.getInfo(baseSubRegionInfo.getId());
        if (!baseSubRegionInfo.getSubRegionName().endsWith("区")) {
            baseSubRegionInfo.setSubRegionName(baseSubRegionInfo.getSubRegionName() + "区");
        }
        List<BaseSubRegionInfo> list = baseSubRegionInfoMapper.selectSubRegionName(baseSubRegionInfo.getSubRegionName(), baseSubRegionInfo.getRegionNo());
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(baseSubRegionInfo.getId())) {
                list.remove(i);
            }
        }
        if (list.size() > 0) {
            return  CommonResult.ResponseResult(0, baseSubRegionInfo.getSubRegionName()+"分区名称已存在");
        }
        List<BaseSubRegionInfo> list2 = baseSubRegionInfoMapper.selectSubRegionNo(baseSubRegionInfo.getSubRegionNo(), baseSubRegionInfo.getRegionNo());
        for (int i = 0; i < list2.size(); i++) {
            if (list2.get(i).getId().equals(baseSubRegionInfo.getId())) {
                list2.remove(i);
            }
        }
        //查看是否有存在的分区编号，若存在则不能添加
        if (list2.size() > 0) {
            return  CommonResult.ResponseResult(0, baseSubRegionInfo.getSubRegionNo()+"分区编号已存在");
        } else {
            String name = baseSubRegionInfo.getSubRegionName();
            if (!name.endsWith("区")) {
                baseSubRegionInfo.setSubRegionName(baseSubRegionInfo.getSubRegionName() + "区");
            }
            no = baseSubRegionInfoMapper.update(baseSubRegionInfo);
        }
        //修改房屋
        List<BaseBuildInfo> baseBuildInfos = baseBuildInfoMapper.selectSubRegionAll(baseSubRegionInfo.getId());
        for (BaseBuildInfo b : baseBuildInfos) {
            int num = baseHouseInfoMapper.selectHouse(b.getId());
            if (num != 0) {
                if (!baseSubRegionInfo.getSubRegionName().equals(oldBase.getSubRegionName())) {
                    baseHouseInfoMapper.updateHouseSit(baseSubRegionInfo.getSubRegionName(), oldBase.getSubRegionName(), b.getId(), null, null);
                    if (no == NumberEnum.ZERO.getNum()) {
                        throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
                    }
                }
            }
        }
        return CommonResult.ResponseResult(no);
    }

    @Override
    public CommonResult delete(String id) {
        int num = 0;
        int count = baseBuildInfoMapper.selectSub(id);
        if (count > 0) {
            return CommonResult.ResponseResult(0, "该分区有自然幢，无法删除");
        } else {
            num = baseBuildInfoMapper.selectSubAll(id);
            if (num > 0) {
             num=  baseSubRegionInfoMapper.updateState(id);
            } else {
                num = baseSubRegionInfoMapper.delete(id);
            }
            return CommonResult.ResponseResult(num);
        }

    }

    @Override
    public PageInfo<BaseSubRegionInfo> getList(Page page, BaseSubRegionInfo baseSubRegionInfo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseSubRegionInfo> list = baseSubRegionInfoMapper.getList(baseSubRegionInfo);
        PageInfo<BaseSubRegionInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public BaseSubRegionInfo getInfo(String id) {
        return baseSubRegionInfoMapper.getInfo(id);
    }

    @Override
    public List<BaseSubRegionInfo> getAllList(BaseSubRegionInfo baseSubRegionInfo) {
        return baseSubRegionInfoMapper.getList(baseSubRegionInfo);
    }

    /**
     * 下拉列表
     *
     * @param id
     * @return
     */
    @Override
    public List<BaseSubRegionInfo> selectDown(String id) {
        return baseSubRegionInfoMapper.selectDown(id);
    }
}
